#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <cerrno>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;

#define Pipepath "./fifo"
#define Mode 0666
class Fifo
{
public:
    Fifo(const char *path = Pipepath)
        : _path(path)
    {
        umask(0);
        int n = mkfifo(_path.c_str(), Mode);
        if (n == 0)
        {
            cout << "touch fifo success" << endl;
        }
        else
        {
            cout << "fail to fifo errno is " << errno << " strerror is " << strerror(errno) << endl;
        }
    }
    ~Fifo()
    {
        int n = unlink(Pipepath);
        if (n == 0)
        {
            cout << "fifo remove" << endl;
        }
        else
        {
            cout << "fail to remove fifo errno is " << errno << " strerror is " << strerror(errno) << endl;
        }
    }

private:
    string _path;
};

int Serveropenfifo()
{
    int rfd = open(Pipepath, O_RDONLY);
    if (rfd < 0)
    {
        cout << "fail to ropen fifo errno is " << errno << " strerror is " << strerror(errno) << endl;
        return 1;
    }
    return rfd;
}
int clientopenfifo()
{
    int wfd = open(Pipepath, O_WRONLY);
    if (wfd < 0)
    {
        cout << "fail to wopen fifo errno is " << errno << " strerror is " << strerror(errno) << endl;
        return 1;
    }
    return wfd;
}

void wakeup(int wfd)
{
    char ch='A';
    write(wfd,&ch,sizeof(ch));
}
void wait(int rfd)
{
    char buffer[10]={0};
    read(rfd,buffer,sizeof(buffer));
}